<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 9, 2013
 * Time: 7:53:07 PM
 * To change this template use File | Settings | File Templates.
 */

class EmployeeController extends BaseController {
    function actionTest() {

    }

    function actionSection() {
        if ($this->userPermission["MENU.EMP.DEP"] == 0) //check user privilege
            $this->urlRedirect('home');
        $this->render('department');
    }

    function actionEmploy() {
        if ($this->userPermission["EMP.EMP"] == 0) //check user privilege
            $this->urlRedirect('home');
        // $department = new Department();
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();
        $parentInstituteList = $institute->GetInstituteListMenu();
        $categoryList = $institute->getEmpCategoryOptions();
        $this->render('employ', array('parentInstitute' => $parentInstituteList, 'category' => $categoryList, 'institute' => $instituteList));
    }

    function actionInstitute() {
        if ($this->userPermission["MENU.EMP.INSTITUTE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $institute = new Institute();
        $parentInstituteList = $institute->getParentInstituteOptions();
        $categoryList = $institute->getCategoryOptions();
        $this->render('institute', array('categoryOptions' => $categoryList, 'parentInstitute' => $parentInstituteList));
    }

    function actionInstCategory() {
        if ($this->userPermission["MENU.EMP.INSTITUTE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $this->render('instCategory');
    }

    function actionEmpCategory() {
        if ($this->userPermission["MENU.EMP.EMPCATEGORY"] == 0) //check user privilege
            $this->urlRedirect('home');
        $this->render('empCategory');
    }

    function actionEmpMap() {
        if ($this->userPermission["EMP.EMP"] == 0) //check user privilege
            $this->urlRedirect('home');
        // $department = new Department();
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();

        $parentInstituteList = $institute->GetInstituteListMenu();
        $categoryList = $institute->getEmpCategoryOptions();
        $this->render('empMap', array('parentInstitute' => $parentInstituteList, 'category' => $categoryList, 'institute' => $instituteList));
    }

    function actionCardList() {
        if ($this->userPermission["EMP.EMP"] == 0) //check user privilege
            $this->urlRedirect('home');
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();
        $parentInstituteList = $institute->GetInstituteListMenu();
        $this->render('empCardList', array('parentInstitute' => $parentInstituteList, 'institute' => $instituteList));
    }

    function actionCheckNic() {
        $employ = new Employ();
        $nic = $_POST['nic'];
        $id = $_POST['id'];
        $_message = $employ->validateNic($nic, $id);
        $data = array('message' => $_message);
        echo json_encode($data);
    }

    function actionCheckTldCode() {
        $card = new TldCard();
        $code = $_POST['code'];
        $_message = $card->validateTldCard($code);
        $data = array('message' => $_message);
        echo json_encode($data);
    }

    function actionGetEmployList() {
        $emp = new Employ();
        $sectionId = $_POST['sectionId'];
        $optionEmp = $emp->getEmployOptions($sectionId);
        echo $optionEmp;
    }

    function actionGetSectionList() {
        $ins = new Institute();
        $instituteId = $_POST['instituteId'];
        $optionSection = $ins->getSectionOptions($instituteId);
        echo $optionSection;
    }

    function actionGetEmploy() {
        $employId = $_POST['employId'];
        $employ = new Employ();
        $data = $employ->getEmploy($employId);
        echo json_encode($data);
    }

    function actionGetInstitute() {
        $instituteId = $_POST['instituteId'];
        $institute = new Institute();
        $data = $institute->getInstitute($instituteId);
        echo json_encode($data);
    }

    function actionGetEmpIns() {
        $employId = $_POST['employId'];
        $instituteId = $_POST['insId'];
        $sectionId = $_POST['secId'];
        $emp = new Employ();
        $data = $emp->getEmpIns($employId, $instituteId, $sectionId);
        echo json_encode($data);
    }

    function actionAddEmpMap() {
        $empId = $_POST['empId'];
        $id = $_POST['id'];
        $instituteId = $_POST['institute_id'];
        $sectionId = $_POST['section_id'];
        $categoryId = $_POST['category_id'];
        $oldToDate = $_POST['old_toDate'];
        $fromDate = $_POST['fromDate'];
        if ($oldToDate == '') {
            $oldToDate = date('Y-m-d');
        }
        if ($fromDate == '') {
            $fromDate = date('Y-m-d');
        }
        $employ = new Employ();
        $data = $employ->updateInstitute($id, $empId, $instituteId, $sectionId, $categoryId, $oldToDate, $fromDate);

        if (is_bool($data)) {
            if ($data) {
                $_message = "Successsfuly added new Employ ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            $_message = "Successsfuly changed Employ institute details ! ";
            $_type = "success";
        }

        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);

    }

    function actionAdd() {
        if ($this->userPermission["EMP.EMP"] == 0) //check user privilege
            $this->urlRedirect('home');
        $employ = new Employ();
        $employ->id = $_POST["id"];
        $employ->fullName = $_POST['fullName'];
        $employ->initName = $_POST['initName'];
        $employ->title = $_POST['title'];
        $employ->instituteId = $_POST['institute_id'];
        $employ->sectionId = $_POST['section_id'];
        $employ->email = $_POST['email'];
        $employ->categoryId = $_POST['category_id'];
        $employ->designation = $_POST['designation'];
        $employ->nic = $_POST['nic'];
        // $employ->epfNo = $_POST['epfNo'];
        $employ->gender = $_POST['gender'];
        $employ->birthday = $_POST['birthday'];
        $employ->address_p1 = $_POST['streetAddress1'];
        $employ->address_p2 = $_POST['streetAddress2'];
        $employ->address_p3 = $_POST['city'] . " " . $_POST['poBox'];
        $employ->address_t1 = $_POST['tempStreetAddress1'];
        $employ->address_t2 = $_POST['tempStreetAddress2'];
        $employ->address_t3 = $_POST['tempCity'] . " " . $_POST['tempPoBox'];
        $employ->phoneMobile = $_POST['phoneMobile'];
        $employ->phoneOffice = $_POST['phoneOffice'];
        $employ->phoneHome = $_POST['phoneHome'];
        $employ->status = $_POST['status'];
        $employ->pic_src = ""; //$_POST[''];


        $i = $employ->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Employ ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Employ information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ";
                $_type = "error";
            }
        }

        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionAddDep() {
        if ($this->userPermission["EMP.DEP.SAVE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $department = new Department();
        $department->departmentName = $_POST["sectionName"];
        $department->departmentCode = $_POST["sectionCode"];
        $department->status = $_POST["status"];
        $department->id = $_POST["id"];
        $i = $department->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Section ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionAddInst() {
        if ($this->userPermission["INST.SAVE"] == 0) //check user privilege
            $data = array('message' => "No privilage", 'type' => "error");
        else {

            $institute = new Institute();
            $institute->instituteName = $_POST["instituteName"];
            $institute->instituteCode = $_POST["instituteCode"];
            $institute->instituteAddress = $_POST["instituteAddress"];
            $institute->phone = $_POST["phone"];
            $institute->email = $_POST["email"];
            $institute->fax = $_POST["fax"];
            $institute->contactPerson = $_POST["contactPerson"];
            $institute->category = $_POST["category"];
            $institute->parentInstitute = $_POST["parentInstitute"];
            $institute->regDate = $_POST["regDate"];
            $institute->hasSubInst = $_POST["hasSubInst"];
            $institute->headName = $_POST["headName"];
            $institute->headAddress = $_POST["headAddress"];
            $institute->headEmail = $_POST["headEmail"];
            $institute->headPhone = $_POST["headPhone"];
            $institute->rpoName = $_POST["rpoName"];
            $institute->rpoAddress = $_POST["rpoAddress"];
            $institute->rpoPhone = $_POST["rpoPhone"];
            $institute->rpoEmail = $_POST["rpoEmail"];
            $institute->status = $_POST["status"];
            $institute->id = $_POST["id"];
            $i = $institute->save();
            if (is_bool($i)) {
                if ($i) {
                    $_message = "Successsfuly added new institute ! ";
                    $_type = "success";
                } else {
                    $_message = "Unsuccesssfuly insert ! ";
                    $_type = "error";
                }

            } else {
                if ($i == -1) {
                    $_message = "No data was changed !";
                    $_type = "info";
                } else if ($i == 1) {
                    $_message = "Successsfuly updated institute information ! ";
                    $_type = "success";
                } else if ($i == 0) {
                    $_message = " Unsuccesssfuly update ! ";
                    $_type = "error";
                }
            }
            $data = array('message' => $_message, 'type' => $_type);
        }
        echo json_encode($data);
    }

    function actionAddInstCategory() {
        if ($this->userPermission["INST.SAVE"] == 0) //check user privilege
            $data = array('message' => "No privilage", 'type' => "error");
        else {

            $instCategory = new InstituteCategory();
            $instCategory->name = $_POST["categoryName"];
            $instCategory->code = $_POST["code"];
            $instCategory->status = $_POST["status"];
            $instCategory->id = $_POST["id"];
            $i = $instCategory->save();
            if (is_bool($i)) {
                if ($i) {
                    $_message = "Successsfuly added new institute ! ";
                    $_type = "success";
                } else {
                    $_message = "Unsuccesssfuly insert ! ";
                    $_type = "error";
                }

            } else {
                if ($i == -1) {
                    $_message = "No data was changed !";
                    $_type = "info";
                } else if ($i == 1) {
                    $_message = "Successsfuly updated institute information ! ";
                    $_type = "success";
                } else if ($i == 0) {
                    $_message = " Unsuccesssfuly update ! ";
                    $_type = "error";
                }
            }
            $data = array('message' => $_message, 'type' => $_type);
        }
        echo json_encode($data);
    }

    function actionAddEmpCategory() {
        if ($this->userPermission["EMP.CATEGORY.ADD"] == 0) //check user privilege
            $data = array('message' => "No privilage", 'type' => "error");
        else {

            $empCategory = new EmployCategory();
            $empCategory->name = $_POST["categoryName"];
            $empCategory->code = $_POST["code"];
            $empCategory->status = $_POST["status"];
            $empCategory->id = $_POST["id"];
            $i = $empCategory->save();
            if (is_bool($i)) {
                if ($i) {
                    $_message = "Successsfuly added new institute ! ";
                    $_type = "success";
                } else {
                    $_message = "Unsuccesssfuly insert ! ";
                    $_type = "error";
                }

            } else {
                if ($i == -1) {
                    $_message = "No data was changed !";
                    $_type = "info";
                } else if ($i == 1) {
                    $_message = "Successsfuly updated institute information ! ";
                    $_type = "success";
                } else if ($i == 0) {
                    $_message = " Unsuccesssfuly update ! ";
                    $_type = "error";
                }
            }
            $data = array('message' => $_message, 'type' => $_type);
        }
        echo json_encode($data);
    }

    function actionGridDep() {
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["sectionName"]))
            $name = $_GET['sectionName'];
        else
            $name = "";
        if (isset($_GET["status"]))
            $status = $_GET['status'];

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "code", "status");

        $section = new Department();
        $xml = $section->getGrid($posStart, $count, $name, $status, $fields[$_GET["orderBy"]], $_GET["direction"]);
        //$xml="this is test";
        header("Content-type:text/xml");
        print $xml;
    }

    function actionGridEmploy() {
        if ($this->userPermission["EMP.EMP"] == 0) //check user privilege
            $this->urlRedirect('home');
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["section_id"]))
            $sectionId = $_GET['section_id'];
        else
            $userName = "";
        if (isset($_GET["name"]))
            $name = $_GET['name'];
        else
            $name = "";
        if (isset($_GET["nic"]))
            $nic = $_GET['nic'];
        else
            $nic = "";
        if (isset($_GET["institute"]))
            $instituteId = $_GET['institute'];
        else
            $sectionId = "";
        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("epf_no", "epf_no", "section_id", "full_name", "nic");

        $employ = new Employ();
        $xml = $employ->getGrid($posStart, $count, $instituteId, $sectionId, $name, $nic, $fields[$_GET["orderBy"]], $_GET["direction"]);
        header("Content-type:text/xml");
        print $xml;

    }


    public function actionGridLocation() {
        if ($this->userPermission["EMP.LOCATION"] == 0) //check user privilege
            $this->urlRedirect('home');
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["locationName"]))
            $name = $_GET['locationName'];
        else
            $name = "";
        if (isset($_GET["status"]))
            $status = $_GET['status'];

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "name", "status");

        $location = new Location();
        $xml = $location->getGrid($posStart, $count, $name, $status, $fields[$_GET["orderBy"]], $_GET["direction"]);
        header("Content-type:text/xml");
        print $xml;

    }

    function actionGridInstitute() {
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["instituteName"]))
            $name = $_GET['instituteName'];
        else
            $name = "";
        if (isset($_GET["status"]))
            $status = $_GET['status'];

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "code", "status");

        $institute = new Institute();
        $xml = $institute->getGrid($posStart, $count, $name, $status, $fields[$_GET["orderBy"]], $_GET["direction"]);
        //$xml="this is test";
        header("Content-type:text/xml");
        print $xml;
    }

    public function actionGridInstCategory() {
        if ($this->userPermission["EMP.INSTCATEGORY"] == 0) //check user privilege
            $this->urlRedirect('home');
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["categoryName"]))
            $name = $_GET['categoryName'];
        else
            $name = "";
        if (isset($_GET["status"]))
            $status = $_GET['status'];

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "name", "status");

        $instCategory = new InstituteCategory();
        $xml = $instCategory->getGrid($posStart, $count, $name, $status, $fields[$_GET["orderBy"]], $_GET["direction"]);
        header("Content-type:text/xml");
        print $xml;

    }

    public function actionGridEmpCategory() {
        if ($this->userPermission["MENU.EMP.EMPCATEGORY"] == 0) //check user privilege
            $this->urlRedirect('home');
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["categoryName"]))
            $name = $_GET['categoryName'];
        else
            $name = "";
        if (isset($_GET["status"]))
            $status = $_GET['status'];

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "name", "status");

        $empCategory = new EmployCategory();
        $xml = $empCategory->getGrid($posStart, $count, $name, $status, $fields[$_GET["orderBy"]], $_GET["direction"]);
        header("Content-type:text/xml");
        print $xml;

    }
}
